<?php

namespace app\common\model\auth;

use app\common\model\BaseModel;

class AuthAdminConfigModel extends BaseModel
{
    protected $table = 't_auth_admin_config';

    const TYPE_ADMIN_SITE_THEME = 1;
    public $type = [
        self::TYPE_ADMIN_SITE_THEME => '站点主题配置'
    ];

    /**
     * @param int $type
     * @param int $adminId
     * @return array|false
     */
    public function getConfigInfo($type, $adminId)
    {
        if (!isset($this->type[$type])) {
            return $this->setError('未知的类型');
        }

        $info = $this->getAdminConfig($type, $adminId);
        if (!$info) {
            return $this->setError('配置项信息不存在或已删除');
        }

        $info['type_attr'] = $this->type[$info['type']];
        $info['content'] = json_decode($info['content'], true);

        return $info;
    }

    /**
     * 编辑-配置内容项
     * @param array $postData
     * @param array $adminInfo
     * @return bool
     */
    public function editByContent($postData, $adminInfo)
    {
        $info = $this->getAdminConfig($postData['type'], $adminInfo['id']);
        if (!$info) {
            return $this->setError('配置项信息不存在或已删除');
        }

        $adminConfigInfo = $this->where('type', $postData['type'])->where('admin_id', $adminInfo['id'])->find();
        if ($adminConfigInfo) {
            $this->where('id', $adminConfigInfo['id'])->update([
                'content' => $postData['content'],
                'update_time' => date('Y-m-d H:i:s')
            ]);
        } else {
            $this->insert([
                'type' => $info['type'],
                'admin_id' => $adminInfo['id'],
                'describe' => $info['describe'],
                'content' => $postData['content'],
                'create_admin_id' => $adminInfo['id'],
                'create_time' => date('Y-m-d H:i:s')
            ]);
        }

        return $this->getConfigInfo($info['type'], $adminInfo['id']);
    }

    /**
     * 重置配置
     * @param array $postData
     * @param array $adminInfo
     * @return bool
     */
    public function resetting($postData, $adminInfo)
    {
        $info = $this->where('type', $postData['type'])->where('admin_id', 0)->find();
        if (!$info) {
            return $this->setError('配置项信息不存在或已删除');
        }

        $adminConfigInfo = $this->where('type', $postData['type'])->where('admin_id', $adminInfo['id'])->find();
        if ($adminConfigInfo) {
            $this->where('id', $adminConfigInfo['id'])->update([
                'content' => $info['content'],
                'update_time' => date('Y-m-d H:i:s')
            ]);
        } else {
            $this->insert([
                'type' => $info['type'],
                'admin_id' => $adminInfo['id'],
                'describe' => $info['describe'],
                'content' => $info['content'],
                'create_admin_id' => $adminInfo['id'],
                'create_time' => date('Y-m-d H:i:s')
            ]);
        }

        return $this->getConfigInfo($info['type'], $adminInfo['id']);
    }

    /**
     * @param int $type
     * @param int $adminId
     * @return
     */
    public function getAdminConfig($type, $adminId)
    {
        $info = $this->where('type', $type)
            ->where('admin_id', $adminId)
            ->field('type,describe,content')
            ->find();
        if (empty($info)) {
            $info = $this->where('type', $type)
                ->where('admin_id', 0)
                ->field('type,describe,content')
                ->find();
        }

        return $info;
    }

}